﻿// -----------------------------------------------------------------------
// <copyright file="ImageHelper.cs" company="FCIS">
// You are welcome to do whatever you want with the code in this file
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.WindowsForms
{
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Extension class for Image.
    /// </summary>
    public static class ImageHelper
    {
        /// <summary>
        /// Extension method returns a bitmap value of the image model
        /// </summary>
        /// <param name="image">The image model to be converted</param>
        /// <returns>Bitmap value of the image model</returns>
        public static System.Drawing.Bitmap ToBitmap(this ImagePackage.Model.Image image)
        {
            Bitmap bitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            // Copy the bytes to the bitmap object
            Marshal.Copy(image.RawPixels, 0, bitmapData.Scan0, image.RawPixels.Length);
            bitmap.UnlockBits(bitmapData);

            return bitmap;
        }
    }
}